import axios from 'axios'



const instance = axios.create({
    baseURL: "http://localhost:3001",
    timeout: 3000
})

instance.interceptors.request.use(
    function(config) {
        //请求头检测，若无则无法发送
        console.log("拦截到的请求为：")
        console.log(config)
        //配置token参数继续请求
        return config//不配置则无法继续发送
    },
    function(err) {
        console.log("网络请求错误")
        return Promise.reject(err)
    }
)

export default function(options) {
    
    return new Promise((resolve, reject) => {
       if(options.method == 'GET' || ''){
           instance.get(options.url,{
               params: options.params || {},
               data: options.data || {}
           }).then(res => {
                resolve(res)
           }).catch(err => {
               reject(err)
           })
       }else if(options.method == 'POST') {
           instance.post(options.url, options.check = {}, {
               params: options.params || {},
               data: options.data || {}
           }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
       }else {
           console.log("请传入正确的参数options")
       }
    }
    )
}

